<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class QuestionModel extends Model
{
	
	
	/**
	 * 获取问题信息
	 *
	 * @param int $qstID
	 * @return array
	 */
	public function getQuestionByID($qstID)
	{
		$map = array('question_id' => $qstID);
		$question = $this->where($map)->find();
		return $question;
	}


	/**
	 * 获取相似问题
	 *
	 * @param string $title
	 * @param int $limit
	 * @return array
	 */
	public function getSimilarQuestions($title, $limit=10)
	{
		$questions = array();
	    if ('en-us' == get_site_config('default_lang'))
		{
			$match  = "MATCH (title,details) AGAINST ('{$title}')";
        	$sql = "SELECT *,{$match} AS similar FROM __TABLE__ WHERE {$where} {$match} ORDER BY similar DESC LIMIT {$limit}";
        	$questions = $this->query($sql);
		}
		else
		{
    		$map   = array('title' => array('like', '%'.$title.'%'));
    		$order = array('question_id' => 'desc');
    		$questions = $this->where($map)->order($order)->limit($limit)->select();
		}
		return $questions;
	}


	/**
	 * 获取问题相关tags
	 *
	 * @param int $qstID
	 * @return array
	 */
	public function getQuestionTags($qstID)
	{
		$Dao = D('QuestionTag');
		$prefix = $this->tablePrefix;
		$map = array('question_id' => $qstID);
		$join = "{$prefix}tag ON {$prefix}question_tag.tag_id={$prefix}tag.tag_id";
		$results = $Dao->join($join)->where($map)->select();
		return $results;
	}


	/**
	 * 获取问题相关tagID
	 *
	 * @access public
	 * @param integer $qstID 问题ID
	 * @return array $tagID 一维数组
	 */
	public function getQuestionTagIDs($qstID)
	{
		$Model  = M('QuestionTag');
		$tagIDs = array();
		$ids = $Model->where('question_id='.$qstID)->select();
		if ($ids)
		{
			foreach ($ids as $value)
			{
				$tagIDs[] = $value['tag_id'];
			}
		}
		return $tagIDs;
	}


    /**
     * 通过审核
     *
     * @access public
     * @param $qstID array
     * @return null
    */
	public function approveQuestionByID($qstID)
	{
	    $where = array('question_id' => array('in', $qstID));
		$this->where($where)->setField('is_visible', 1);
	}


    /**
     * 否决审核
     * @param $qstID array
     * @return null
    */
	public function disapproveQuestionByID($qstID)
	{
	    $where = array('question_id' => array('in', $qstID));
		$this->where($where)->setField('is_visible', -1);
	}


	/**
     * 还原问题
     * @param $qstID array
     * @return null
    */
	public function restoreQuestionByID($qstID)
	{
	    $where = array('question_id' => array('in', $qstID));
		$this->where($where)->setField('is_visible', 1);
	}


	/**
     * 锁定问题
     * @param $qstID array
     * @return null
    */
	public function lockQuestionByID($qstID)
	{
	    $where = array('question_id' => array('in', $qstID));
		$this->where($where)->setField('is_locked', 1);
	}


	/**
     * 解锁问题
     * @param $qstID array
     * @return null
    */
	public function unlockQuestionByID($qstID)
	{
	    $where = array('question_id' => array('in', $qstID));
		$this->where($where)->setField('is_locked', 0);
	}


	/**
     * 删除问题
     *
     * @access public
     * @param array $qstID
     * @param integer $direct 是否直接删除
     * @return null
     */
	public function deleteQuestionByID($qstID, $direct=0)
	{
	    $prefix = $this->tablePrefix;
	    $Star = D('QuestionStar');
	    $Follow = D('WatchedQuestionFollow');
	    $Answer = D('Answer');
	    $Comment = D('Comment');

	    //递减所属分类计数
		$Category = D('Category');
		$question = $this->find($qstID);
		$Category->updateQuestionCount('question_count', 'category_id='.$question['category_id'], 1);

		//递减相关tag计数
		$sql = "UPDATE {$prefix}tag SET question_count=question_count-1 WHERE tag_id IN (SELECT tag_id FROM {$prefix}question_tag WHERE question_id={$qstID})";
		$this->query($sql);

		$recycle = get_site_config('recycle_bin_enable');
		$where = array('question_id' => array('in', $qstID));
		if ($direct || !$recycle)
		{
			$Star->where($where)->delete();
			$Follow->where($where)->delete();
			$Answer->where($where)->delete();
			$Comment->where($where)->delete();
			$this->where($where)->delete();
		}
		else
		{
			$Answer->where($where)->setField('is_visible', -1);
			$Comment->where($where)->setField('is_visible', -1);
			$this->where($where)->setField('is_visible', -1);
		}
	}


	/**
     * 删除问题
     *
     * @access public
     * @param array $userID
     * @param integer $direct 是否直接删除
     * @return null
     */
	public function deleteQuestionByUserID($userID, $direct=0)
	{
		$recycle = get_site_config('recycle_bin_enable');
		$prefix  = $this->tablePrefix;
		$userIDs = implode(',', $userID);

		if ($direct || !$recycle)
		{
		    // todo: 删除附件

		    //删除相关星号标记问题
		    $sql = "DElETE FROM {$prefix}question_star WHERE question_id IN (SELECT question_id FROM {$prefix}question WHERE poster_id IN ({$userIDs}))";
		    $this->query($sql);

		    //删除相关关注问题
		    $sql = "DElETE FROM {$prefix}question_star WHERE question_id IN (SELECT question_id FROM {$prefix}question WHERE poster_id In ({$userIDs}))";
		    $this->query($sql);

		    //删除问题标签
    	    $sql = "DElETE FROM {$prefix}question_tag WHERE question_id IN (SELECT question_id FROM {$prefix}question WHERE poster_id IN({$userIDs}))";
    	    $this->query($sql);

		    //删除问题
		    $map = array('poster_id' => array('in', $userID));
			$this->where($map)->delete();
		}
		else
		{
		    //"删除"问题
		    $map = array('poster_id' => array('in', $userID));
			$this->where($map)->setField('is_visible', -1);
		}
	}


	/**
	 * 更新问题答案计数
	 *
	 * @access public
	 * @param integer $questionID
	 * @return integer $count
	 */
	public function updateAnswerCount($questionID)
	{
	    $Answer = M('Answer');
	    $map = array('question_id' => $questionID, 'is_visible' => 1);
		$count = $Answer->where($map)->count();

		$where = array('question_id' => $questionID);
		$data = array('answer_count' => intval($count));
		$this->where($where)->save($data);
		return $count;
	}


	/**
	 * 更新问题评论计数
	 *
	 * @access public
	 * @param integer $questionID
	 * @return integer $count
	 */
	public function updateCommentCount($questionID)
	{
	    $Comment = M('Comment');
	    $map = array('related_id' => $questionID, 'type' => 'Q', 'is_visible' => 1);
		$count = $Comment->where($map)->count();

		$where = array('question_id' => $questionID);
		$data = array('comment_count' => intval($count));
		$this->where($where)->save($data);
		return $count;
	}


	/**
	 * 更新问题星号标计数
	 *
	 * @access public
	 * @param integer $questionID
	 * @return integer $count
	 */
	public function updateStarCount($qstID)
	{
	    $Star = M('QuestionStar');
	    $map = array('question_id' => $qstID);
	    $count = $Star->where($map)->count();

	    $where = array('question_id' => $qstID);
	    $data = array('star_count' => intval($count));
	    $this->where($where)->save($data);
	    return $count;
	}


    /**
	 * 更新问题关注计数
	 *
	 * @access public
	 * @param integer $questionID
	 * @return integer $count
	 */
	public function updateFollowCount($qstID)
	{
	    $Follow = D('QuestionFollow');
	    $map = array('question_id' => $qstID);
	    $count = $Follow->where($map)->count();

	    $where = array('question_id' => $qstID);
	    $data = array('follow_count' => intval($count));
	    $this->where($where)->save($data);
	    return $count;
	}


	/**
	 * 更新问题浏览数
	 *
	 * @access public
	 * @param integer $questionID
	 * @param string  $lazy
	 * @return null
	 */
	public function updateViewCount($questionID, $lazy='', $time=300)
	{
		$default = get_site_config('lazy_update_enable');
		$lazy = !empty($lazy) ? true : $default;
		$where = array('question_id' => $questionID);
		if ($lazy)
		{
		    $this->switchModel('Adv')->setLazyInc('view_count', $where, 1, $time);
		}
		else
		{
			$this->setInc('view_count', $where, 1);
		}
	}

}

/* End of file QuestionModel.class.php */
/* Location: ./App/Lib/Model/QuestionModel.class.php */